package com.fcscs.cservice.client.main.reservation;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.TimeField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreConstants.ValidationListener;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.BaseFormDialog;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.IEFriendlyDateField;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.reservation.AdvancedSearchItemsGWTEnum;
import com.fcscs.cservice.client.common.model.reservation.AdvancedSearchItemsGWTEnumModelData;
import com.fcscs.cservice.client.common.model.reservation.ReservationStatusModelData;
import com.fcscs.cservice.client.common.model.reservation.RoomStatusModelData;
import com.fcscs.cservice.domain.model.ReservationConfView;
import com.fcscs.cservice.domain.model.ReservationConfView.ReservationStatus;
import com.fcscs.cservice.domain.model.ReservationConfView.RoomStatus;
import com.fcscs.cservice.domain.model.ReservationView;
import com.fcscs.share.gwt.common.ShareGWTConstants;
import com.google.gwt.event.dom.client.KeyCodes;

public class AdvancedSearchDialog extends BaseFormDialog<ReservationView> implements
		FormSubmitListener<ReservationView> {
	private MyFormPanel formPanel;
	private FormSubmitListener<ReservationView> listener;
	private static AdvancedSearchDialog INSTANCE;
	private boolean isFirstLoad;
	private ReservationConfView reservationConf;
	
	protected EasyClearCombobox<AdvancedSearchItemsGWTEnumModelData> searchItemCombo1;
	protected EasyClearCombobox<AdvancedSearchItemsGWTEnumModelData> searchItemCombo2;
	protected EasyClearCombobox<AdvancedSearchItemsGWTEnumModelData> searchItemCombo3;
	
	protected TextField<String> firstName1;
	protected TextField<String> firstName2;
	protected TextField<String> firstName3;
	protected TextField<String> lastName1;
	protected TextField<String> lastName2;
	protected TextField<String> lastName3;
	protected EasyClearCombobox<ReservationStatusModelData> reservationStatusCombo1;
	protected EasyClearCombobox<ReservationStatusModelData> reservationStatusCombo2;
	protected EasyClearCombobox<ReservationStatusModelData> reservationStatusCombo3;
	protected TextField<String> roomNo1;
	protected TextField<String> roomNo2;
	protected TextField<String> roomNo3;
	protected TextField<String> memberShipNo1;
	protected TextField<String> memberShipNo2;
	protected TextField<String> memberShipNo3;
	protected ListStore<ReservationStatusModelData> reservationStatusListStore;
	protected EasyClearCombobox<RoomStatusModelData> roomStatusCombo1;
	protected EasyClearCombobox<RoomStatusModelData> roomStatusCombo2;
	protected EasyClearCombobox<RoomStatusModelData> roomStatusCombo3;
	protected TextField<String> baggageNo1;
	protected TextField<String> baggageNo2;
	protected TextField<String> baggageNo3;
	protected ListStore<RoomStatusModelData> roomStatusListStore;
	protected IEFriendlyDateField dateField1;
	protected IEFriendlyDateField dateField2;
	protected IEFriendlyDateField dateField3;
	protected TimeField timeField1;
	protected TimeField timeField2;
	protected TimeField timeField3;
	
	public static AdvancedSearchDialog getInstance(
			FormSubmitListener<ReservationView> listener,
			ReservationConfView reservationConf){
		if(null == INSTANCE){
			INSTANCE = new AdvancedSearchDialog();
		}
		INSTANCE.listener = listener;
		INSTANCE.reservationConf = reservationConf;
		INSTANCE.setHeading(CServiceConstants.CSERVICE_CONSTANTS.advancedSearch());
		return INSTANCE;
	}
	
	private AdvancedSearchDialog(){
		isFirstLoad = true;
		formPanel = new MyFormPanel(this);
		formPanel.setId("AdvancedSearchDialog_formPanel");
		add(formPanel);
		setSize(540, 220);
	}
	
	private class MyFormPanel extends FcsFormPanel<ReservationView>{
		private ListStore<AdvancedSearchItemsGWTEnumModelData> searchItemListStore;
		private ListStore<AdvancedSearchItemsGWTEnumModelData> searchItemListStore1;
		private ListStore<AdvancedSearchItemsGWTEnumModelData> searchItemListStore2;
		private ListStore<AdvancedSearchItemsGWTEnumModelData> searchItemListStore3;
		private LayoutContainer lc1;
		private LayoutContainer lc2;
		private LayoutContainer lc3;
		
		private FormButton fb;

		protected MyFormPanel(FormSubmitListener<ReservationView> l) {
			super(l);
			setHeaderVisible(false);
			
			searchItemListStore = new ListStore<AdvancedSearchItemsGWTEnumModelData>();
			for(AdvancedSearchItemsGWTEnum asi : AdvancedSearchItemsGWTEnum.values()){
				searchItemListStore.add(new AdvancedSearchItemsGWTEnumModelData(asi));
			}
			
			searchItemListStore1 = new ListStore<AdvancedSearchItemsGWTEnumModelData>();
			searchItemListStore2 = new ListStore<AdvancedSearchItemsGWTEnumModelData>();
			searchItemListStore3 = new ListStore<AdvancedSearchItemsGWTEnumModelData>();
			
			lc1 = new LayoutContainer();
			lc1.setId("AdvancedSearchDialog_lc1");
			lc1.setLayout(new ColumnLayout());
			
			lc2 = new LayoutContainer();
			lc2.setId("AdvancedSearchDialog_lc2");
			lc2.setLayout(new ColumnLayout());
			
			lc3 = new LayoutContainer();
			lc3.setId("AdvancedSearchDialog_lc3");
			lc3.setLayout(new ColumnLayout());
			
			searchItemCombo1 = new EasyClearCombobox<AdvancedSearchItemsGWTEnumModelData>();
			searchItemCombo1.setId("AdvancedSearchDialog_searchItemCombo1");
			searchItemCombo1.setStore(searchItemListStore1);
			searchItemCombo1.setValue(searchItemListStore.getAt(0));
			searchItemCombo1.setTriggerAction(TriggerAction.ALL);
			searchItemCombo1.setDisplayField(CoreConstants.FULLTNAME);
			searchItemCombo1.setForceSelection(true);
			searchItemCombo1.setAllowBlank(false);
			searchItemCombo1.addSelectionChangedListener(new SelectionChangedListener<AdvancedSearchItemsGWTEnumModelData>(){
				@Override
				public void selectionChanged(
						SelectionChangedEvent<AdvancedSearchItemsGWTEnumModelData> se) {
					searchItemView(searchItemCombo1.getValue().getEnum().getIndex(), 1);
					buildStore();
				}
			});
			
			
			searchItemCombo2 = new EasyClearCombobox<AdvancedSearchItemsGWTEnumModelData>();
			searchItemCombo2.setId("AdvancedSearchDialog_searchItemCombo2");
			searchItemCombo2.setStore(searchItemListStore2);
			searchItemCombo2.setValue(searchItemListStore.getAt(1));
			searchItemCombo2.setTriggerAction(TriggerAction.ALL);
			searchItemCombo2.setDisplayField(CoreConstants.FULLTNAME);
			searchItemCombo2.setForceSelection(true);
			searchItemCombo2.setAllowBlank(false);
			searchItemCombo2.addSelectionChangedListener(new SelectionChangedListener<AdvancedSearchItemsGWTEnumModelData>(){
				@Override
				public void selectionChanged(
						SelectionChangedEvent<AdvancedSearchItemsGWTEnumModelData> se) {
					searchItemView(searchItemCombo2.getValue().getEnum().getIndex(), 2);
					buildStore();
				}
			});
			
			searchItemCombo3 = new EasyClearCombobox<AdvancedSearchItemsGWTEnumModelData>();
			searchItemCombo3.setId("AdvancedSearchDialog_searchItemCombo3");
			searchItemCombo3.setStore(searchItemListStore3);
			searchItemCombo3.setValue(searchItemListStore.getAt(2));
			searchItemCombo3.setTriggerAction(TriggerAction.ALL);
			searchItemCombo3.setDisplayField(CoreConstants.FULLTNAME);
			searchItemCombo3.setForceSelection(true);
			searchItemCombo3.setAllowBlank(false);
			searchItemCombo3.addSelectionChangedListener(new SelectionChangedListener<AdvancedSearchItemsGWTEnumModelData>(){
				@Override
				public void selectionChanged(
						SelectionChangedEvent<AdvancedSearchItemsGWTEnumModelData> se) {
					searchItemView(searchItemCombo3.getValue().getEnum().getIndex(), 3);
					buildStore();
				}
			});
			
			lc1.add(searchItemCombo1, new ColumnData(150));
			lc2.add(searchItemCombo2, new ColumnData(150));
			lc2.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "20px");
			lc3.add(searchItemCombo3, new ColumnData(150));
			lc3.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "20px");
			
			firstName1 = new TextField<String>();
			firstName1.setId("AdvancedSearchDialog_firstName1");
			firstName1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			firstName1.setEmptyText(CoreConstants.CORE_CONSTANTS.firstname());
			firstName1.setVisible(false);
			firstName1.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(!firstName1.isVisible()){
						return null;
					}else if(null != value && (!value.trim().isEmpty()) &&(value.trim().length() < 2 || value.trim().length() > 255)){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.nameValid(CoreConstants.CORE_CONSTANTS.firstname(), 2, 255);
					}
					return null;
				}
			});
			
			firstName2 = new TextField<String>();
			firstName2.setId("AdvancedSearchDialog_firstName2");
			firstName2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			firstName2.setEmptyText(CoreConstants.CORE_CONSTANTS.firstname());
			firstName2.setVisible(false);
			firstName2.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(!firstName2.isVisible()){
						return null;
					}else if(null != value && (!value.trim().isEmpty()) &&(value.trim().length() < 2 || value.trim().length() > 255)){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.nameValid(CoreConstants.CORE_CONSTANTS.firstname(), 2, 255);
					}
					return null;
				}
			});
			
			firstName3 = new TextField<String>();
			firstName3.setId("AdvancedSearchDialog_firstName3");
			firstName3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			firstName3.setEmptyText(CoreConstants.CORE_CONSTANTS.firstname());
			firstName3.setVisible(false);
			firstName3.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(!firstName3.isVisible()){
						return null;
					}else if(null != value && (!value.trim().isEmpty()) &&(value.trim().length() < 2 || value.trim().length() > 255)){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.nameValid(CoreConstants.CORE_CONSTANTS.firstname(), 2, 255);
					}
					return null;
				}
			});
			
			lc1.add(firstName1, new ColumnData(150));
			lc2.add(firstName2, new ColumnData(150));
			lc3.add(firstName3, new ColumnData(150));
			
			lastName1 = new TextField<String>();
			lastName1.setId("AdvancedSearchDialog_lastName1");
			lastName1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			lastName1.setEmptyText(CoreConstants.CORE_CONSTANTS.lastname());
			lastName1.setVisible(false);
			lastName1.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(!lastName1.isVisible()){
						return null;
					}else if(null != value && (!value.trim().isEmpty()) &&(value.trim().length() < 2 || value.trim().length() > 255)){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.nameValid(CoreConstants.CORE_CONSTANTS.firstname(), 2, 255);
					}
					return null;
				}
			});
			
			lastName2 = new TextField<String>();
			lastName2.setId("AdvancedSearchDialog_lastName2");
			lastName2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			lastName2.setEmptyText(CoreConstants.CORE_CONSTANTS.lastname());
			lastName2.setVisible(false);
			lastName2.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(!lastName2.isVisible()){
						return null;
					}else if(null != value && (!value.trim().isEmpty()) &&(value.trim().length() < 2 || value.trim().length() > 255)){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.nameValid(CoreConstants.CORE_CONSTANTS.firstname(), 2, 255);
					}
					return null;
				}
			});
			
			lastName3 = new TextField<String>();
			lastName3.setId("AdvancedSearchDialog_lastName3");
			lastName3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			lastName3.setEmptyText(CoreConstants.CORE_CONSTANTS.lastname());
			lastName3.setVisible(false);
			lastName3.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(!lastName3.isVisible()){
						return null;
					}else if(null != value && (!value.trim().isEmpty()) &&(value.trim().length() < 2 || value.trim().length() > 255)){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.nameValid(CoreConstants.CORE_CONSTANTS.firstname(), 2, 255);
					}
					return null;
				}
			});
			
			lc1.add(lastName1, new ColumnData(150));
			lc2.add(lastName2, new ColumnData(150));
			lc3.add(lastName3, new ColumnData(150));
			
			reservationStatusListStore = new ListStore<ReservationStatusModelData>();
			reservationStatusCombo1 = new EasyClearCombobox<ReservationStatusModelData>();
			reservationStatusCombo1.setId("AdvancedSearchDialog_reservationStatusCombobox1");
			reservationStatusCombo1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			reservationStatusCombo1.setEmptyText(CServiceConstants.CSERVICE_CONSTANTS.reservationStatus());
			reservationStatusCombo1.setDisplayField(CServiceConstants.STATUS);
			reservationStatusCombo1.setTriggerAction(TriggerAction.ALL);
			reservationStatusCombo1.setStore(reservationStatusListStore);
			reservationStatusCombo1.setForceSelection(true);
			reservationStatusCombo1.setEditable(false);
			reservationStatusCombo1.setVisible(false);
			
			reservationStatusCombo2 = new EasyClearCombobox<ReservationStatusModelData>();
			reservationStatusCombo2.setId("AdvancedSearchDialog_reservationStatusCombo2");
			reservationStatusCombo2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			reservationStatusCombo2.setEmptyText(CServiceConstants.CSERVICE_CONSTANTS.reservationStatus());
			reservationStatusCombo2.setDisplayField(CServiceConstants.STATUS);
			reservationStatusCombo2.setTriggerAction(TriggerAction.ALL);
			reservationStatusCombo2.setStore(reservationStatusListStore);
			reservationStatusCombo2.setForceSelection(true);
			reservationStatusCombo2.setEditable(false);
			reservationStatusCombo2.setVisible(false);
			
			reservationStatusCombo3 = new EasyClearCombobox<ReservationStatusModelData>();
			reservationStatusCombo3.setId("AdvancedSearchDialog_reservationStatusCombo3");
			reservationStatusCombo3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			reservationStatusCombo3.setEmptyText(CServiceConstants.CSERVICE_CONSTANTS.reservationStatus());
			reservationStatusCombo3.setDisplayField(CServiceConstants.STATUS);
			reservationStatusCombo3.setTriggerAction(TriggerAction.ALL);
			reservationStatusCombo3.setStore(reservationStatusListStore);
			reservationStatusCombo3.setForceSelection(true);
			reservationStatusCombo3.setEditable(false);
			reservationStatusCombo3.setVisible(false);
			
			lc1.add(reservationStatusCombo1, new ColumnData(150));
			lc2.add(reservationStatusCombo2, new ColumnData(150));
			lc3.add(reservationStatusCombo3, new ColumnData(150));
			
			roomNo1 = new TextField<String>();
			roomNo1.setId("AdvancedSearchDialog_roomNo1");
			roomNo1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			roomNo1.setVisible(false);
			
			roomNo2 = new TextField<String>();
			roomNo2.setId("AdvancedSearchDialog_roomNo2");
			roomNo2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			roomNo2.setVisible(false);
			
			roomNo3 = new TextField<String>();
			roomNo3.setId("AdvancedSearchDialog_roomNo3");
			roomNo3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			roomNo3.setVisible(false);
			
			lc1.add(roomNo1, new ColumnData(150));
			lc2.add(roomNo2, new ColumnData(150));
			lc3.add(roomNo3, new ColumnData(150));
			
			memberShipNo1 = new TextField<String>();
			memberShipNo1.setId("AdvancedSearchDialog_memberShipNo1");
			memberShipNo1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			memberShipNo1.setVisible(false);
			memberShipNo1.addListener(Events.KeyUp, new Listener<FieldEvent>(){
				@Override
				public void handleEvent(FieldEvent be) {
					countDetailTextArea(be, memberShipNo1);
				}
			});
			
			memberShipNo2 = new TextField<String>();
			memberShipNo2.setId("AdvancedSearchDialog_memberShipNo2");
			memberShipNo2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			memberShipNo2.setVisible(false);
			memberShipNo2.addListener(Events.KeyUp, new Listener<FieldEvent>(){
				@Override
				public void handleEvent(FieldEvent be) {
					countDetailTextArea(be, memberShipNo2);
				}
			});
			
			memberShipNo3 = new TextField<String>();
			memberShipNo3.setId("AdvancedSearchDialog_memberShipNo3");
			memberShipNo3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			memberShipNo3.setVisible(false);
			memberShipNo3.addListener(Events.KeyUp, new Listener<FieldEvent>(){
				@Override
				public void handleEvent(FieldEvent be) {
					countDetailTextArea(be, memberShipNo3);
				}
			});
			
			lc1.add(memberShipNo1, new ColumnData(150));
			lc2.add(memberShipNo2, new ColumnData(150));
			lc3.add(memberShipNo3, new ColumnData(150));
			
			roomStatusListStore = new ListStore<RoomStatusModelData>();
			roomStatusCombo1 = new EasyClearCombobox<RoomStatusModelData>();
			roomStatusCombo1.setId("AdvancedSearchDialog_roomStatusCombo1");
			roomStatusCombo1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			roomStatusCombo1.setDisplayField(CServiceConstants.STATUS);
			roomStatusCombo1.setTriggerAction(TriggerAction.ALL);
			roomStatusCombo1.setStore(roomStatusListStore);
			roomStatusCombo1.setForceSelection(true);
			roomStatusCombo1.setEditable(false);
			roomStatusCombo1.setVisible(false);
			
			roomStatusCombo2 = new EasyClearCombobox<RoomStatusModelData>();
			roomStatusCombo2.setId("AdvancedSearchDialog_roomStatusCombo2");
			roomStatusCombo2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			roomStatusCombo2.setDisplayField(CServiceConstants.STATUS);
			roomStatusCombo2.setTriggerAction(TriggerAction.ALL);
			roomStatusCombo2.setStore(roomStatusListStore);
			roomStatusCombo2.setForceSelection(true);
			roomStatusCombo2.setEditable(false);
			roomStatusCombo2.setVisible(false);
			
			roomStatusCombo3 = new EasyClearCombobox<RoomStatusModelData>();
			roomStatusCombo3.setId("AdvancedSearchDialog_roomStatusCombo3");
			roomStatusCombo3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			roomStatusCombo3.setDisplayField(CServiceConstants.STATUS);
			roomStatusCombo3.setTriggerAction(TriggerAction.ALL);
			roomStatusCombo3.setStore(roomStatusListStore);
			roomStatusCombo3.setForceSelection(true);
			roomStatusCombo3.setEditable(false);
			roomStatusCombo3.setVisible(false);
			
			lc1.add(roomStatusCombo1, new ColumnData(150));
			lc2.add(roomStatusCombo2, new ColumnData(150));
			lc3.add(roomStatusCombo3, new ColumnData(150));
			
			baggageNo1 = new TextField<String>();
			baggageNo1.setId("AdvancedSearchDialog_baggageNo1");
			baggageNo1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			baggageNo1.setVisible(false);
			
			baggageNo2 = new TextField<String>();
			baggageNo2.setId("AdvancedSearchDialog_baggageNo2");
			baggageNo2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			baggageNo2.setVisible(false);
			
			baggageNo3 = new TextField<String>();
			baggageNo3.setId("AdvancedSearchDialog_baggageNo3");
			baggageNo3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			baggageNo3.setVisible(false);
			
			lc1.add(baggageNo1, new ColumnData(150));
			lc2.add(baggageNo2, new ColumnData(150));
			lc3.add(baggageNo3, new ColumnData(150));
			
			dateField1 = new IEFriendlyDateField();
			dateField1.setId("AdvancedSearchDialog_dateField1");
			dateField1.setEmptyText(CoreConstants.CORE_CONSTANTS.date());
			dateField1.setMessageTarget("tooltip");
			dateField1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			dateField1.setVisible(false);
			dateField1.setEditable(false);
//			dateField1.setMinValue(getDateBeforeOrAfterCurrentDay(-30));
//			dateField1.setMaxValue(getDateBeforeOrAfterCurrentDay(30));
			dateField1.addListener(Events.Change, new Listener<BaseEvent>(){
				@Override
				public void handleEvent(BaseEvent be) {
				    if(dateField1.isVisible() && dateField1.getValue() == null && timeField1.getValue() != null){
						dateField1.forceInvalid(CoreConstants.CORE_CONSTANTS.requiredField());
						return;
					}else if((timeField1.getRawValue() != null)
							&& (!timeField1.getRawValue().trim().isEmpty())
							&& !isValidTime(timeField1.getRawValue().trim())){
						dateField1.clearInvalid();
						timeField1.forceInvalid(CServiceConstants.CSERVICE_CONSTANTS.validTime());
						return;
					}else if(dateField1.isVisible() && dateField1.getValue() != null){
						dateField1.clearInvalid();
						return;
					}
				}
			});
			
			dateField2 = new IEFriendlyDateField();
			dateField2.setId("AdvancedSearchDialog_dateField2");
			dateField2.setEmptyText(CoreConstants.CORE_CONSTANTS.date());
			dateField2.setMessageTarget("tooltip");
			dateField2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			dateField2.setVisible(false);
			dateField2.setEditable(false);
			dateField2.addListener(Events.Change, new Listener<BaseEvent>(){
				@Override
				public void handleEvent(BaseEvent be) {
					if(dateField2.isVisible() && dateField2.getValue() == null && timeField2.getValue() != null){
						dateField2.forceInvalid(CoreConstants.CORE_CONSTANTS.requiredField());
						return;
					}else if((timeField2.getRawValue() != null)
							&& (!timeField2.getRawValue().trim().isEmpty())
							&& !isValidTime(timeField2.getRawValue().trim())){
						dateField2.clearInvalid();
						timeField2.forceInvalid(CServiceConstants.CSERVICE_CONSTANTS.validTime());
						return;
					}else if(dateField2.isVisible() && dateField2.getValue() != null){
						dateField2.clearInvalid();
						return;
					}
				}
			});
			
			dateField3 = new IEFriendlyDateField();
			dateField3.setId("AdvancedSearchDialog_dateField3");
			dateField3.setEmptyText(CoreConstants.CORE_CONSTANTS.date());
			dateField3.setMessageTarget("tooltip");
			dateField3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			dateField3.setVisible(false);
			dateField3.setEditable(false);
			dateField3.addListener(Events.Change, new Listener<BaseEvent>(){
				@Override
				public void handleEvent(BaseEvent be) {
					if(dateField3.isVisible() && dateField3.getValue() == null && timeField3.getValue() != null){
						dateField3.forceInvalid(CoreConstants.CORE_CONSTANTS.requiredField());
						return;
					}else if((timeField3.getRawValue() != null)
							&& (!timeField3.getRawValue().trim().isEmpty())
							&& !isValidTime(timeField3.getRawValue().trim())){
						dateField3.clearInvalid();
						timeField3.forceInvalid(CServiceConstants.CSERVICE_CONSTANTS.validTime());
						return;
					}else if(dateField3.isVisible() && dateField3.getValue() != null){
						dateField3.clearInvalid();
						return;
					}
				}
			});
			
			lc1.add(dateField1, new ColumnData(150));
			lc2.add(dateField2, new ColumnData(150));
			lc3.add(dateField3, new ColumnData(150));
			
			timeField1 = new TimeField();
			timeField1.setId("AdvancedSearchDialog_timeField1");
			timeField1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			timeField1.addListener(Events.OnBlur, new Listener<BaseEvent>(){
				@Override
				public void handleEvent(BaseEvent be) {
					if(timeField1.isVisible() && dateField1.getValue() == null){
						dateField1.forceInvalid(CoreConstants.CORE_CONSTANTS.requiredField());
						return;
					}
					else if((timeField1.getRawValue() != null)
							&& (!timeField1.getRawValue().trim().isEmpty())
							&& !isValidTime(timeField1.getRawValue().trim())){
						timeField1.forceInvalid(CServiceConstants.CSERVICE_CONSTANTS.validTime());
						return;
					}else{
						timeField1.clearInvalid();
						return;
					}
				}
			});
			timeField1.setVisible(false);
			
			timeField2 = new TimeField();
			timeField2.setId("AdvancedSearchDialog_timeField2");
			timeField2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			timeField2.addListener(Events.OnBlur, new Listener<BaseEvent>(){
				@Override
				public void handleEvent(BaseEvent be) {
					if(timeField2.isVisible() && dateField2.getValue() == null){
						dateField2.forceInvalid(CoreConstants.CORE_CONSTANTS.requiredField());
						return;
					}else if((timeField2.getRawValue() != null)
							&& (!timeField2.getRawValue().trim().isEmpty())
							&& !isValidTime(timeField2.getRawValue().trim())){
						timeField2.forceInvalid(CServiceConstants.CSERVICE_CONSTANTS.validTime());
						return;
					}else{
						timeField2.clearInvalid();
						return;
					}
				}
			});
			timeField2.setVisible(false);
			
			timeField3 = new TimeField();
			timeField3.setId("AdvancedSearchDialog_timeField3");
			timeField3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			timeField3.addListener(Events.OnBlur, new Listener<BaseEvent>(){
				@Override
				public void handleEvent(BaseEvent be) {
					if(timeField3.isVisible() && dateField3.getValue() == null){
						dateField3.forceInvalid(CoreConstants.CORE_CONSTANTS.requiredField());
						return;
					}else if((timeField3.getRawValue() != null)
							&& (!timeField3.getRawValue().trim().isEmpty())
							&& !isValidTime(timeField3.getRawValue().trim())){
						timeField3.forceInvalid(CServiceConstants.CSERVICE_CONSTANTS.validTime());
						return;
					}else{
						timeField3.clearInvalid();
						return;
					}
				}
			});
			timeField3.setVisible(false);
			
			lc1.add(timeField1, new ColumnData(150));
			lc2.add(timeField2, new ColumnData(150));
			lc3.add(timeField3, new ColumnData(150));
			
			fb = new FormButton(CoreConstants.CORE_CONSTANTS.ok());
			fb.setId("AdvancedSearchDialog_Save_button");
			fb.setStyleAttribute(CServiceConstants.CSS_MARGINRIGHT, "200px");
			fb.addSelectionListener(new SelectionListener<ButtonEvent>(){
				@Override
				public void componentSelected(ButtonEvent ce) {
					isValid(new ValidationListener() {

						@Override
						public void onValidate(boolean good) {
							if(good){
								submit(SubmitType.DEFAULT);
								fb.removeStyleName(CoreConstants.CSS_FORM_BUTTON_FOCUS);
							}
						}
					});
				}
			});
			
			final FormButton cbtn = new FormButton(CoreConstants.CORE_CONSTANTS.cancel());
			cbtn.setId("AdvancedSearchDialog_cbtn_button");
			cbtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
				@Override
				public void componentSelected(ButtonEvent ce) {
					AdvancedSearchDialog.this.hide();
					cbtn.removeStyleName(CoreConstants.CSS_FORM_BUTTON_FOCUS);
				}
			});
			
			
			add(lc1);
			add(lc2);
			add(lc3);
			getButtonBar().add(fb);
			getButtonBar().add(cbtn);
			searchItemView(0, 1);
			searchItemView(1, 2);
			searchItemView(2, 3);
			
			buildStore();
		}

		private void buildStore() {
			searchItemListStore1.removeAll();
			for(int i = 0; i < searchItemListStore.getCount() ; i++){
				if((searchItemCombo2.getValue().getEnum().getIndex() 
					!= searchItemListStore.getAt(i).getEnum().getIndex())
					&&
					(searchItemCombo3.getValue().getEnum().getIndex() 
							!= searchItemListStore.getAt(i).getEnum().getIndex())){
					searchItemListStore1.add(searchItemListStore.getAt(i));
				}
			}
			
			searchItemListStore3.removeAll();
			for(int i = 0; i < searchItemListStore.getCount() ; i++){
				if((searchItemCombo1.getValue().getEnum().getIndex() 
						!= searchItemListStore.getAt(i).getEnum().getIndex())
						&&
						(searchItemCombo2.getValue().getEnum().getIndex() 
								!= searchItemListStore.getAt(i).getEnum().getIndex())){
					searchItemListStore3.add(searchItemListStore.getAt(i));
				}
			}
			
			searchItemListStore2.removeAll();
			for(int i = 0; i < searchItemListStore.getCount() ; i++){
				if((searchItemCombo1.getValue().getEnum().getIndex() 
						!= searchItemListStore.getAt(i).getEnum().getIndex())
						&&
						(searchItemCombo3.getValue().getEnum().getIndex() 
								!= searchItemListStore.getAt(i).getEnum().getIndex())){
					searchItemListStore2.add(searchItemListStore.getAt(i));
				}
			}
		}

		@Override
		public void updateForm(ReservationView entity) {
			for(ReservationStatus rs : reservationConf.getReservationStatusList()){
				reservationStatusListStore.add(new ReservationStatusModelData(rs));
			}
			for(RoomStatus roomS : reservationConf.getRoomStatusList()){
				roomStatusListStore.add(new RoomStatusModelData(roomS));
			}
		}

		@Override
		public void populateEntity(ReservationView entity) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void setFocus() {
			// TODO Auto-generated method stub
			
		}
		
		private void searchItemView(int code,int row){
			switch(code){
			    case 0:
			    	switch(row){
			    	    case 1:
			    	    	firstName1.clear();
			    	    	lastName1.clear();
			    	    	firstName1.setVisible(true);
			    	    	lastName1.setVisible(true);
			    	    	reservationStatusCombo1.setVisible(false);
			    	    	roomNo1.setVisible(false);
			    	    	memberShipNo1.setVisible(false);
			    	    	roomStatusCombo1.setVisible(false);
			    	    	baggageNo1.setVisible(false);
			    	    	dateField1.setVisible(false);
			    	    	timeField1.setVisible(false);
			    	    	break;
			    	    case 2:
			    	    	firstName2.clear();
			    	    	lastName2.clear();
			    	    	firstName2.setVisible(true);
			    	    	lastName2.setVisible(true);
			    	    	reservationStatusCombo2.setVisible(false);
			    	    	roomNo2.setVisible(false);
			    	    	memberShipNo2.setVisible(false);
			    	    	roomStatusCombo2.setVisible(false);
			    	    	baggageNo2.setVisible(false);
			    	    	dateField2.setVisible(false);
			    	    	timeField2.setVisible(false);
			    	    	break;
			    	    case 3:
			    	    	firstName3.clear();
			    	    	lastName3.clear();
			    	    	firstName3.setVisible(true);
			    	    	lastName3.setVisible(true);
			    	    	reservationStatusCombo3.setVisible(false);
			    	    	roomNo3.setVisible(false);
			    	    	memberShipNo3.setVisible(false);
			    	    	roomStatusCombo3.setVisible(false);
			    	    	baggageNo3.setVisible(false);
			    	    	dateField3.setVisible(false);
			    	    	timeField3.setVisible(false);
			    	    	break;
			    	}
			    	break;
			    case 1:
			    	switch(row){
		    	    case 1:
		    	    	firstName1.setVisible(false);
		    	    	lastName1.setVisible(false);
		    	    	reservationStatusCombo1.setVisible(true);
		    	    	roomNo1.setVisible(false);
		    	    	memberShipNo1.setVisible(false);
		    	    	roomStatusCombo1.setVisible(false);
		    	    	baggageNo1.setVisible(false);
		    	    	dateField1.setVisible(false);
		    	    	timeField1.setVisible(false);
		    	    	break;
		    	    case 2:
		    	    	firstName2.setVisible(false);
		    	    	lastName2.setVisible(false);
		    	    	reservationStatusCombo2.setVisible(true);
		    	    	roomNo2.setVisible(false);
		    	    	memberShipNo2.setVisible(false);
		    	    	roomStatusCombo2.setVisible(false);
		    	    	baggageNo2.setVisible(false);
		    	    	dateField2.setVisible(false);
		    	    	timeField2.setVisible(false);
		    	    	break;
		    	    case 3:
		    	    	firstName3.setVisible(false);
		    	    	lastName3.setVisible(false);
		    	    	reservationStatusCombo3.setVisible(true);
		    	    	roomNo3.setVisible(false);
		    	    	memberShipNo3.setVisible(false);
		    	    	roomStatusCombo3.setVisible(false);
		    	    	baggageNo3.setVisible(false);
		    	    	dateField3.setVisible(false);
		    	    	timeField3.setVisible(false);
		    	    	break;
		    	    }
		    	    break;
			    case 2:
			    	switch(row){
		    	    case 1:
		    	    	firstName1.setVisible(false);
		    	    	lastName1.setVisible(false);
		    	    	reservationStatusCombo1.setVisible(false);
		    	    	roomNo1.setVisible(true);
		    	    	memberShipNo1.setVisible(false);
		    	    	roomStatusCombo1.setVisible(false);
		    	    	baggageNo1.setVisible(false);
		    	    	dateField1.setVisible(false);
		    	    	timeField1.setVisible(false);
		    	    	break;
		    	    case 2:
		    	    	firstName2.setVisible(false);
		    	    	lastName2.setVisible(false);
		    	    	reservationStatusCombo2.setVisible(false);
		    	    	roomNo2.setVisible(true);
		    	    	memberShipNo2.setVisible(false);
		    	    	roomStatusCombo2.setVisible(false);
		    	    	baggageNo2.setVisible(false);
		    	    	dateField2.setVisible(false);
		    	    	timeField2.setVisible(false);
		    	    	break;
		    	    case 3:
		    	    	firstName3.setVisible(false);
		    	    	lastName3.setVisible(false);
		    	    	reservationStatusCombo3.setVisible(false);
		    	    	roomNo3.setVisible(true);
		    	    	memberShipNo3.setVisible(false);
		    	    	roomStatusCombo3.setVisible(false);
		    	    	baggageNo3.setVisible(false);
		    	    	dateField3.setVisible(false);
		    	    	timeField3.setVisible(false);
		    	    	break;
		    	    }
		    	    break;
			    case 3:
			    	switch(row){
		    	    case 1:
		    	    	firstName1.setVisible(false);
		    	    	lastName1.setVisible(false);
		    	    	reservationStatusCombo1.setVisible(false);
		    	    	roomNo1.setVisible(false);
		    	    	memberShipNo1.setVisible(true);
		    	    	roomStatusCombo1.setVisible(false);
		    	    	baggageNo1.setVisible(false);
		    	    	dateField1.setVisible(false);
		    	    	timeField1.setVisible(false);
		    	    	break;
		    	    case 2:
		    	    	firstName2.setVisible(false);
		    	    	lastName2.setVisible(false);
		    	    	reservationStatusCombo2.setVisible(false);
		    	    	roomNo2.setVisible(false);
		    	    	memberShipNo2.setVisible(true);
		    	    	roomStatusCombo2.setVisible(false);
		    	    	baggageNo2.setVisible(false);
		    	    	dateField2.setVisible(false);
		    	    	timeField2.setVisible(false);
		    	    	break;
		    	    case 3:
		    	    	firstName3.setVisible(false);
		    	    	lastName3.setVisible(false);
		    	    	reservationStatusCombo3.setVisible(false);
		    	    	roomNo3.setVisible(false);
		    	    	memberShipNo3.setVisible(true);
		    	    	roomStatusCombo3.setVisible(false);
		    	    	baggageNo3.setVisible(false);
		    	    	dateField3.setVisible(false);
		    	    	timeField3.setVisible(false);
		    	    	break;
		    	    }
		    	    break;
			    case 4:
			    	switch(row){
		    	    case 1:
		    	    	firstName1.setVisible(false);
		    	    	lastName1.setVisible(false);
		    	    	reservationStatusCombo1.setVisible(false);
		    	    	roomNo1.setVisible(false);
		    	    	memberShipNo1.setVisible(false);
		    	    	roomStatusCombo1.setVisible(true);
		    	    	baggageNo1.setVisible(false);
		    	    	dateField1.setVisible(false);
		    	    	timeField1.setVisible(false);
		    	    	break;
		    	    case 2:
		    	    	firstName2.setVisible(false);
		    	    	lastName2.setVisible(false);
		    	    	reservationStatusCombo2.setVisible(false);
		    	    	roomNo2.setVisible(false);
		    	    	memberShipNo2.setVisible(false);
		    	    	roomStatusCombo2.setVisible(true);
		    	    	baggageNo2.setVisible(false);
		    	    	dateField2.setVisible(false);
		    	    	timeField2.setVisible(false);
		    	    	break;
		    	    case 3:
		    	    	firstName3.setVisible(false);
		    	    	lastName3.setVisible(false);
		    	    	reservationStatusCombo3.setVisible(false);
		    	    	roomNo3.setVisible(false);
		    	    	memberShipNo3.setVisible(false);
		    	    	roomStatusCombo3.setVisible(true);
		    	    	baggageNo3.setVisible(false);
		    	    	dateField3.setVisible(false);
		    	    	timeField3.setVisible(false);
		    	    	break;
		    	    }
		    	    break;
			    case 5:
			    	switch(row){
		    	    case 1:
		    	    	firstName1.setVisible(false);
		    	    	lastName1.setVisible(false);
		    	    	reservationStatusCombo1.setVisible(false);
		    	    	roomNo1.setVisible(false);
		    	    	memberShipNo1.setVisible(false);
		    	    	roomStatusCombo1.setVisible(false);
		    	    	baggageNo1.setVisible(true);
		    	    	dateField1.setVisible(false);
		    	    	timeField1.setVisible(false);
		    	    	break;
		    	    case 2:
		    	    	firstName2.setVisible(false);
		    	    	lastName2.setVisible(false);
		    	    	reservationStatusCombo2.setVisible(false);
		    	    	roomNo2.setVisible(false);
		    	    	memberShipNo2.setVisible(false);
		    	    	roomStatusCombo2.setVisible(false);
		    	    	baggageNo2.setVisible(true);
		    	    	dateField2.setVisible(false);
		    	    	timeField2.setVisible(false);
		    	    	break;
		    	    case 3:
		    	    	firstName3.setVisible(false);
		    	    	lastName3.setVisible(false);
		    	    	reservationStatusCombo3.setVisible(false);
		    	    	roomNo3.setVisible(false);
		    	    	memberShipNo3.setVisible(false);
		    	    	roomStatusCombo3.setVisible(false);
		    	    	baggageNo3.setVisible(true);
		    	    	dateField3.setVisible(false);
		    	    	timeField3.setVisible(false);
		    	    	break;
		    	    }
		    	    break;
			    case 6:
			    case 7:
			    	switch(row){
		    	    case 1:
		    	    	dateField1.clear();
		    	    	timeField1.clear();
		    	    	firstName1.setVisible(false);
		    	    	lastName1.setVisible(false);
		    	    	reservationStatusCombo1.setVisible(false);
		    	    	roomNo1.setVisible(false);
		    	    	memberShipNo1.setVisible(false);
		    	    	roomStatusCombo1.setVisible(false);
		    	    	baggageNo1.setVisible(false);
		    	    	dateField1.setVisible(true);
		    	    	timeField1.setVisible(true);
		    	    	break;
		    	    case 2:
		    	    	dateField2.clear();
		    	    	timeField2.clear();
		    	    	firstName2.setVisible(false);
		    	    	lastName2.setVisible(false);
		    	    	reservationStatusCombo2.setVisible(false);
		    	    	roomNo2.setVisible(false);
		    	    	memberShipNo2.setVisible(false);
		    	    	roomStatusCombo2.setVisible(false);
		    	    	baggageNo2.setVisible(false);
		    	    	dateField2.setVisible(true);
		    	    	timeField2.setVisible(true);
		    	    	break;
		    	    case 3:
		    	    	dateField3.clear();
		    	    	timeField3.clear();
		    	    	firstName3.setVisible(false);
		    	    	lastName3.setVisible(false);
		    	    	reservationStatusCombo3.setVisible(false);
		    	    	roomNo3.setVisible(false);
		    	    	memberShipNo3.setVisible(false);
		    	    	roomStatusCombo3.setVisible(false);
		    	    	baggageNo3.setVisible(false);
		    	    	dateField3.setVisible(true);
		    	    	timeField3.setVisible(true);
		    	    	break;
		    	    }
		    	    break;
			}
		}
		
//		private void loadtRoomStatus(){
//			roomStatusListStore = null;
//		}
		
		@SuppressWarnings("unchecked")
		protected void countDetailTextArea(FieldEvent be, TextField<String> tf) {
			TextField<String> t = (TextField<String>)be.getField();
			String value = t.getValue() == null ? "" : t.getValue().trim();
			
			// with pressing follow keys ,no need to calculate : DELETE(46)
			// , DOWN , UP , ENTER , ESCAPE , HOME , END , LEFT ,RIGHT , TAB
			// PAGEUP , PAGEDOWN ,

			int newKeyCode = be.getKeyCode();
			int[] noKeys = new int[] { KeyCodes.KEY_DELETE, KeyCodes.KEY_DOWN, KeyCodes.KEY_UP, KeyCodes.KEY_ENTER, KeyCodes.KEY_ESCAPE, KeyCodes.KEY_HOME, KeyCodes.KEY_END, KeyCodes.KEY_LEFT, KeyCodes.KEY_RIGHT, KeyCodes.KEY_PAGEUP, KeyCodes.KEY_PAGEDOWN, KeyCodes.KEY_TAB, KeyCodes.KEY_ALT, KeyCodes.KEY_SHIFT, KeyCodes.KEY_CTRL};
			for (int nokey : noKeys) {
				if (nokey == newKeyCode) {
					return;
				}
			}
			int cc = getCharCount(value);
			
			if (cc>10) {
				tf.setValue(value.substring(0, 7) + "...");
			}else{
				tf.setValue(value);
			}
		}
		
		private int getCharCount(String v) {
			if (v != null) {
				int charCount = 0;
				String[] strs = v.split("\\t|\r|\n");
				for (String str : strs) {
					charCount += str.length();
				}
				return charCount;
			}
			return 0;
		}
	}

	@Override
	protected FcsFormPanel<ReservationView> getFormPanel() {
		return formPanel;
	}

	@Override
	public void formSubmitCallBack(FcsFormPanel<ReservationView> form,
			SubmitType submitType) {
		if (listener != null) {
			listener.formSubmitCallBack(form, submitType);
		}
	}

	@Override
	public void hide() {
		super.hide();
		if (!isFirstLoad) {
			reservationStatusListStore.removeAll();
			roomStatusListStore.removeAll();
			firstName1.clear();
			lastName1.clear();
			reservationStatusCombo1.clear();
			roomNo1.clear();
			memberShipNo1.clear();
			roomStatusCombo1.clear();
			baggageNo1.clear();
			dateField1.clear();
			timeField1.clear();
			firstName2.clear();
			lastName2.clear();
			reservationStatusCombo2.clear();
			roomNo2.clear();
			memberShipNo2.clear();
			roomStatusCombo2.clear();
			baggageNo2.clear();
			dateField2.clear();
			timeField2.clear();
			firstName3.clear();
			lastName3.clear();
			reservationStatusCombo3.clear();
			roomNo3.clear();
			memberShipNo3.clear();
			roomStatusCombo3.clear();
			baggageNo3.clear();
			dateField3.clear();
			timeField3.clear();
		}
		isFirstLoad = false;
	}
	
	private boolean isValidTime(String time){
		boolean isValid = true;
		if(!time.contains(":")){
			isValid = false;
		}else{
			String[] ts= time.split(":");
			if(ts.length != 2){
				isValid = false;
			}else{
				if((ts[0].trim().length() != 2) 
				   || (ts[1].trim().length() != 2)){
					isValid = false;
				}else{
					if((!Character.isDigit(ts[0].trim().charAt(0))) 
						|| (!Character.isDigit(ts[0].trim().charAt(1)))
						|| (!Character.isDigit(ts[1].trim().charAt(0))) 
					    || (!Character.isDigit(ts[1].trim().charAt(1)))){
						isValid = false;
					}else if((Integer.parseInt(ts[0].trim()) > 24)
							 || (Integer.parseInt(ts[1].trim()) > 59)){
						isValid = false;
					}
				}
			}
		}
		return isValid;
	}
	
//	private Date getDateBeforeOrAfterCurrentDay(int days){
//		Date date = new Date();
//		long myTime = date.getTime()/1000 + 24*60*60*days;
//		date.setTime(myTime*1000);
//		return date;
//	}
}
